package com.ymk.mq;

import com.alibaba.fastjson.JSON;
import com.ymk.base.cmd.CmdSign;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.bo.goods.GoodsCatBO;
import com.ymk.bo.member.MemberBO;
import com.ymk.bo.stat.StatMchDayBO;
import com.ymk.bo.stat.StatMchMonthBO;
import com.ymk.bo.stat.StatMchYearBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.code.cmd.MemberCmdCode;
import com.ymk.core.code.cmd.StatMchCmdCode;
import com.ymk.core.constants.SysConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
public class MessageSender {

    private final static Logger logger = LoggerFactory.getLogger(MessageSender.class);

    @Autowired
    private AmqpTemplate rabbitTemplate;

    protected void addMsgData(String queueCode,String data) {
        this.rabbitTemplate.convertAndSend(queueCode, data);
    }

    /**
     * 商品缓存
     */
    public void sendGoodsCacheSync(Integer goodsId, CmdSign reqSign) {
        GoodsBO goodsBO = new GoodsBO();
        goodsBO.setId(goodsId);
        CmdSign cmdSign = CmdSign.Builder
                .newBuild(reqSign)
                .setCmdCode(GoodsCmdCode.GOODS_CACHE)
                .setSource(goodsBO)
                .setToken(reqSign.getToken())
                .build();
        this.addMsgData(RabbitmqCode.GOODS_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 商品分类缓存
     */
    public void sendGoodsCatCacheSync(Integer mchId, CmdSign reqSign) {
        GoodsCatBO goodsCatBO = new GoodsCatBO();
        goodsCatBO.setMchId(mchId);
        CmdSign cmdSign = CmdSign.Builder
                .newBuild(reqSign)
                .setCmdCode(GoodsCmdCode.GOODS_CAT_CACHE)
                .setSource(goodsCatBO)
                .setToken(reqSign.getToken())
                .build();
        this.addMsgData(RabbitmqCode.GOODS_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 商品每日同步
     */
    public void sendGoodsSync(Integer goodsId, CmdSign reqSign) {
        GoodsBO goodsBO = new GoodsBO();
        goodsBO.setId(goodsId);
        CmdSign cmdSign = CmdSign.Builder
                .newBuild(reqSign)
                .setCmdCode(GoodsCmdCode.GOODS_CACHE)
                .setSource(goodsBO)
                .setToken(reqSign.getToken())
                .build();
        this.addMsgData(RabbitmqCode.GOODS_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 会员财务同步
     */
    public void sendMemberFinanceSync(Integer memberId, CmdSign reqSign) {
        MemberBO memberBO = new MemberBO();
        memberBO.setId(memberId);
        CmdSign cmdSign = CmdSign.Builder
                .newBuild(reqSign)
                .setSource(memberBO)
                .setToken(reqSign.getToken())
                .setCmdCode(MemberCmdCode.MEMBER_FINANCE_CACHE)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();
        this.addMsgData(RabbitmqCode.MEMBER_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 每日商户统计
     */
    public void sendStatMchDay(Integer mchId, Date statDate, CmdSign reqSign) {
        StatMchDayBO statMchDayBO = new StatMchDayBO();
        statMchDayBO.setMchId(mchId);
        statMchDayBO.setStatDate(statDate);

        CmdSign cmdSign = CmdSign.Builder.newBuild(reqSign)
                .setCmdCode(StatMchCmdCode.STAT_MCH_DAY_POST)
                .setSource(statMchDayBO)
                .setToken(SysConstants.DEFAULT_ADMIN_TOKEN)
                .build();
        this.addMsgData(RabbitmqCode.MEMBER_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 每月商户统计
     */
    public void sendStatMchMonth(Integer mchId, Integer year ,Integer month, CmdSign reqSign) {
        StatMchMonthBO statMchMonthBO = new StatMchMonthBO();
        statMchMonthBO.setMchId(mchId);
        statMchMonthBO.setYear(year);
        statMchMonthBO.setMonth(month);

        CmdSign cmdSign = CmdSign.Builder.newBuild(reqSign)
                .setCmdCode(StatMchCmdCode.STAT_MCH_MONTH_POST)
                .setSource(statMchMonthBO)
                .build();
        this.addMsgData(RabbitmqCode.MEMBER_QUEUE, JSON.toJSONString(cmdSign));
    }

    /**
     * 每年商户统计
     */
    public void sendStatMchYear(Integer mchId, Integer year , CmdSign reqSign) {
        StatMchYearBO statMchYearBO = new StatMchYearBO();
        statMchYearBO.setMchId(mchId);
        statMchYearBO.setYear(year);

        CmdSign cmdSign = CmdSign.Builder.newBuild(reqSign)
                .setCmdCode(StatMchCmdCode.STAT_MCH_YEAR_POST)
                .setSource(statMchYearBO)
                .build();
        this.addMsgData(RabbitmqCode.MEMBER_QUEUE, JSON.toJSONString(cmdSign));
    }



}
